package com.why.simpleboot.core.core.aop.processor;

import com.why.simpleboot.core.core.aop.interceptor.Interceptor;
import com.why.simpleboot.core.core.aop.proxy.CglibProxy;
import com.why.simpleboot.core.core.aop.proxy.JdkProxy;
import com.why.simpleboot.core.core.config.BeanPostProcessor;

import java.util.List;

/**
 * @program: why-simpleboot-framework
 * @description:产生
 * @author: @why
 * @create: 2021-05-27 07:38
 **/
public class GenerateBeanPostProcessorFactory {
    public static Object getProxyBean(Object bean, List<Interceptor> interceptors) {
        Object wrapperBean = bean;
        BeanPostProcessor postProcessor;
        if (bean.getClass().getInterfaces().length > 0) {
            postProcessor = new JdkPorxyBeanPostProcessor(interceptors);
        } else {
            postProcessor = new CglibBeanPostProcessor(interceptors);
        }
        wrapperBean = postProcessor.postProcessAfterInitialization(wrapperBean);
        return wrapperBean;
    }


    /**
     *
     */

    protected static class JdkPorxyBeanPostProcessor extends AbstractAopBeanPostProcessor {

        public JdkPorxyBeanPostProcessor(List<Interceptor> interceptors) {
            super(interceptors);
        }

        @Override
        public Object wrappedBean(Object bean, Interceptor interceptor) {

            return JdkProxy.wrapperBean(bean, interceptor);
        }
    }









    protected static class CglibBeanPostProcessor extends AbstractAopBeanPostProcessor {
        public CglibBeanPostProcessor(List<Interceptor> interceptors) {
            super(interceptors);
        }

        @Override
        public Object wrappedBean(Object bean, Interceptor interceptor) {

            return CglibProxy.wrapperBean(bean, interceptor);
        }
    }

}
